package exabase.meta;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Map.Entry;
import java.util.logging.Logger;

import exabase.data.Hash;

public class MetaStore {
	private static Logger logger = Logger.getLogger(MetaStore.class.getName());
	protected static MetaStore instance = null;

	protected HashMap<Hash, String> hashToFileMap;

	protected MetaStore() {
		hashToFileMap = new HashMap<Hash, String>();
	}

	public static MetaStore getInstance() {
		if (instance == null) {
			instance = new MetaStore();
		}
		return instance;
	}

	public Set<Meta> getMetaSet() {
		Set<Meta> metas = new HashSet<Meta>();
		for (Entry<Hash, String> entry : hashToFileMap.entrySet()) {
			metas.add(new Meta(entry.getValue()));
		}
		return metas;
	}

	public void addMeta(String metaFileName, Hash hash) {
		logger.finer("adding new meta " + metaFileName + ", " + hash);
		hashToFileMap.put(hash, metaFileName);
	}

	public static void reset() {
		instance = null;
	}

	public void updateMeta(Hash hash, String metaFileName) {
		hashToFileMap.put(hash, metaFileName);
	}

	public void addMeta(Meta meta) {
		addMeta(meta.getMetaFileName(), meta.getHash());
	}
}
